<?PHP
/**
 * 控制器处理类 信息
	 
 */
class Admin_pen_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','信息管理');
		$this->assign('menuid1','1');
		$this->assign('menuid2','4');
		//权限判断
		factory::module('admin')->authSys('pen');
	}
	##################################
	/* 信息管理  */
	##################################
	function index__(){ 
		$pentypes = factory::module('pentype')->load_isleaf();  
		$this->assign('pentypes',$pentypes); 
	}
	function index_list__(){ 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';  
			if(!isset($_POST['pentype_id']))   $_POST['pentype_id']  = '';
			if(!isset($_POST['publishs']))    $_POST['publishs']  = '';  
  	  #查询条件
			$sql_where = ' where    a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.title','b.name' ));
			if($_POST['pentype_id'] != ''){
				$sql_where .= ' and a.pentype_id="'.$_POST['pentype_id'].'" ';
			}
			if($_POST['publishs']!=''){
				$sql_where .= ' and a.publishs= "'.$_POST['publishs'].'"';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_pen` a left join t_pentype b on a.pentype_id=b.id  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.id, 
					a.title,
					a.showcount,
					a.update_date,
					a.create_date,
					a.create_user_id,
					a.publishs,
					a.pentype_id,
					b.name as pentype_name,
					c.username as create_username
				from `t_pen` a  left join t_pentype b on a.pentype_id=b.id left join t_user c on a.create_user_id=c.id
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql); 
				# 拿到类别的全路径
				foreach($rs as &$v){
					$v['pentype_name'] = factory::module('pentype')->findfullpath($v['pentype_id']);
				} 
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}  
	} 
	# 添加
	public function add__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	
			$_POST=format::addslash_deep($_POST);
			try{ 
			  $_POST['content'] = htmlspecialchars(rawurldecode($_POST['content']));  //做转义存储，防止sql注入
			}catch(Exception $e){
				exit('参数错误');
			}
			$db = factory::getDBO(); 
			$db->begin(); 
			
 //for($i=0; $i<500; $i++){
			
			//主表
			$pen_id = $db->insert('pen',array(
				'pentype_id'=>$_POST['pentype_id'],
				'title'=>$_POST['title'],
				'source'=>$_POST['source'],
				'content'=>$_POST['content'],  
				'publishs'=>$_POST['publishs'],
				'acdate'=>$_POST['acdate'],
				'create_user_id'=>factory::module('admin')->getuserid(),
				'create_date'=>'now()',
				'update_user_id'=>factory::module('admin')->getuserid(),
				'update_date'=>'now()'
			));
	 	
			if(!$pen_id){
				url::msgalert('操作失败！');
			}
			
			# 图片分析
			if($_FILES['logo']['name']){
				$UploadFiles = uploadfile::singleton();
				# 做一些大小类型设置 
				$UploadFiles->set_base_directory(factory::getcfg('pen_path'));
				$UploadFiles->set_size(factory::getcfg('pen_maxsize'));
				$UploadFiles->set_extention(factory::getcfg('pen_type')); 
				$UploadFiles->set_file_type($_FILES['logo']['type']);
				$UploadFiles->set_file_name(rand(100000,999999).'.'.preg_replace('/.*\.(.*[^\.].*)*/iU','\\1',$_FILES['logo']['name']));
				$UploadFiles->set_file_size($_FILES['logo']['size']);
				$UploadFiles->set_upfile($_FILES['logo']['tmp_name']);
				$UploadFiles->set_datetime('_'.rand(1,100).'_');
				$UploadFiles->save();
				$db->update('pen',array(
					'id'=>$pen_id,
					'logo'=>$UploadFiles->get_file_path()
				));
			}		
			
 //	}
			$db->commit();
			url::msgalert('添加成功！',url::build('admin_pen','index')); 
		} 

		$pentypes = factory::module('pentype')->load_isleaf(); 
		$this->assign('pentypes',$pentypes); 
	}
	 
	# 浏览
	public function  view__(){   
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_pen` a  where deleted=0 and a.id="'.$id.'" ');
		if(!$rs) exit('没有查到任何记录，请重新操作！'); 
		$this->assign('rs',$rs);
	}
	/**
	 * 删除
	 */
	public function  del__(){ 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$idarr = format::addslash_json(json_decode( stripslashes($_POST['idarr']) ));
		
			$db = factory::getDBO(); 
			foreach($idarr as $id){
					$delete = $db->query("update t_pen set deleted=1 where id=".$id);
					if($delete){ 
						 exit('{success:1,msg:"操作成功！"}'); 
					}else{
						exit('系统繁忙，请稍后再试！');
					}	 
			}
			exit('{success:1,msg:"操作成功！"}'); 
		}
		exit();
	}	
	
	
	
	# 修改
	public function  modify__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST);
			try{ 
			  $_POST['content'] = htmlspecialchars(rawurldecode($_POST['content']));  //做转义存储，防止sql注入
			}catch(Exception $e){
				exit('参数错误');
			}
			$db = factory::getDBO(); 
			$db->begin();
			$pen_id = $_POST['id'];
			$rs_update = array(
				'id'=>$pen_id,
				'pentype_id'=>$_POST['pentype_id'],
				'title'=>$_POST['title'],
				'source'=>$_POST['source'],
				'content'=>$_POST['content'], 
				'publishs'=>$_POST['publishs'], 
				'acdate'=>$_POST['acdate'],
				'update_user_id'=>factory::module('admin')->getuserid(),
				'update_date'=>'now()' 
			);
			if(!isset($_POST['del_logo'])){ 
			}else{
				$rs_update['logo'] = '';
			}
		//	debug::show($rs_update);
			//主表
			if(!$db->update('pen',$rs_update)){
				url::msgalert('操作失败！');
			}
			
			# 图片分析
			if($_FILES['logo']['name']){
				$UploadFiles = uploadfile::singleton();
				# 做一些大小类型设置 
				$UploadFiles->set_base_directory(factory::getcfg('pen_path'));
				$UploadFiles->set_size(factory::getcfg('pen_maxsize'));
				$UploadFiles->set_extention(factory::getcfg('pen_type')); 
				$UploadFiles->set_file_type($_FILES['logo']['type']);
				$UploadFiles->set_file_name(rand(100000,999999).'.'.preg_replace('/.*\.(.*[^\.].*)*/iU','\\1',$_FILES['logo']['name']));
				$UploadFiles->set_file_size($_FILES['logo']['size']);
				$UploadFiles->set_upfile($_FILES['logo']['tmp_name']);
				$UploadFiles->set_datetime('_'.rand(1,100).'_');
				$UploadFiles->save();
				$db->update('pen',array(
					'id'=>$pen_id,
					'logo'=>$UploadFiles->get_file_path()
				));
			}		

			
			
			
			$db->commit();
			url::msgalert('修改成功！',url::build('admin_pen','index')); 
		} 
		$this->assign('menuid1','3');
		$this->assign('nav','信息管理 - 编辑信息');   
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.*,b.username from `t_pen` a left join t_user b on a.create_user_id=b.id  where a.deleted=0 and a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1');
		$this->assign('rs',$rs);
		$pentypes = factory::module('pentype')->load_isleaf(); 
		$this->assign('pentypes',$pentypes); 
	}
}
?>